package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.utils.Arith;
import com.lam.common.utils.StringUtils;
import com.lam.mall.app.vo.ProdCommDataVo;
import com.lam.mall.app.vo.ProdCommVo;
import com.lam.mall.entity.ProdComm;
import com.lam.mall.mapper.ProdCommMapper;
import com.lam.mall.param.ProdCommParam;
import com.lam.mall.service.ProdCommService;

/**
 * 商品评论Service业务层处理
 * 
 * @author admin
 * @date 2023-04-28
 */
@Service
public class ProdCommServiceImpl  implements ProdCommService {

    @Autowired
    private ProdCommMapper prodCommMapper;

    /**
     * 查询商品评论
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public ProdComm selectById(String id) {

        return prodCommMapper.selectById(id);
    }

    /**
     * 查询商品评论列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<ProdComm> selectList(ProdCommParam param) {
    
        QueryWrapper<ProdComm> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(Objects.nonNull(param.getProdId()), "prod_id", param.getProdId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getOrderNumber()), "order_number", param.getOrderNumber());
        queryWrapper.eq(Objects.nonNull(param.getOrderItemId()), "order_item_id", param.getOrderItemId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());

        return prodCommMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品评论
     * 
     * @param prodComm 
     * @return 
     */
    @Override
    @Transactional
    public int insert(ProdComm prodComm) {
    
        return prodCommMapper.insert(prodComm);
    }

    /**
     * 修改商品评论
     * 
     * @param prodComm 
     * @return 
     */
    @Override
    @Transactional
    public int update(ProdComm prodComm) {
    
        return prodCommMapper.updateById(prodComm);
    }

    /**
     * 删除商品评论信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return prodCommMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Override
    public ProdCommDataVo getByProdId(String prodId) {
    	ProdCommDataVo prodCommDataDto=prodCommMapper.getByProdId(prodId);
        //计算出好评率
        if(prodCommDataDto.getPraiseNumber() == 0||prodCommDataDto.getNumber() == 0){
            prodCommDataDto.setPositiveRating(0.0);
        }else{
            prodCommDataDto.setPositiveRating(Arith.mul(Arith.div(prodCommDataDto.getPraiseNumber(),prodCommDataDto.getNumber()),100));
        }
        return prodCommDataDto;
    }
    
    @Override
    public List<ProdCommVo> getPageByUserId(String userId) {
        return prodCommMapper.getPageByUserId(userId);
    }
    
    @Override
    public List<ProdCommVo> getPageByProdId(String prodId, Integer evaluate) {

    	List<ProdCommVo> list = prodCommMapper.getPageByProdId(prodId, evaluate);
    	list.forEach(prodCommDto -> {
            // 匿名评价
            if (prodCommDto.getIsAnonymous() == 1) {
                prodCommDto.setNickName(null);
            }
        });
        return list;
    }
}
